package osResourceMonitor

import (
	"fmt"
	"github.com/shirou/gopsutil/mem"
	"reflect"
)

type SwapEr interface {
	SwapDevice(s any) (any, error)
	SwapMemory(s any) (any, error)
	SwapMemoryWitchContext(s any) (any, error)
}
type VirtualEr interface {
	VirtualMemory(s any) (any, error)
	VirtualMemoryWitchContext(s any) (any, error)
}
type MemEr interface {
	SwapEr
	VirtualEr
}
type LinuxMem struct {
}

func NewMemObject() MemEr {
	var c MemEr
	c = &LinuxMem{}
	return c
}

func (l LinuxMem) SwapDevice(s any) (any, error) {
	var (
		o   []*mem.SwapDevice
		err error
	)
	if o, err = mem.SwapDevices(); err != nil {
		return nil, err
	}
	switch fmt.Sprintf("%v", reflect.TypeOf(s)) {
	case "string":
		switch s.(string) {
		case "name":
			var so []string
			for _, v := range o {
				so = append(so, v.Name)
			}
			return so, nil
		case "all":
			var so []string
			for _, v := range o {
				so = append(so, v.String())
			}
			return so, nil
		case "used":
			var so []uint64
			for _, v := range o {
				so = append(so, v.UsedBytes)
			}
			return so, nil
		case "free":
			var so []uint64
			for _, v := range o {
				so = append(so, v.FreeBytes)
			}
			return so, nil
		case "usage":
			var so []float64
			for _, v := range o {
				usage := float64(v.UsedBytes * 100 / (v.UsedBytes + v.FreeBytes))
				so = append(so, usage)
			}
			return so, nil
		case "total":
			var so []uint64
			for _, v := range o {
				total := v.UsedBytes + v.FreeBytes
				so = append(so, total)
			}
			return so, nil
		case "rema":
			var so []float64
			for _, v := range o {
				rema := float64(v.FreeBytes * 100 / (v.UsedBytes + v.FreeBytes))
				so = append(so, rema)
			}
			return so, nil
		}
	}
	return o, nil
}
func (l LinuxMem) SwapMemoryWitchContext(_ any) (any, error) {
	//mem.SwapMemoryWithContext()
	return nil, nil
}

func (l LinuxMem) SwapMemory(_ any) (any, error) {
	var (
		m   *mem.SwapMemoryStat
		err error
	)
	if m, err = mem.SwapMemory(); err != nil {
		return nil, err
	}
	return m, nil
}
func (l LinuxMem) VirtualMemory(_ any) (any, error) {
	var (
		m   *mem.VirtualMemoryStat
		err error
	)
	if m, err = mem.VirtualMemory(); err != nil {
		return nil, err
	}
	fmt.Println(m)
	return m, nil
}
func (l LinuxMem) VirtualMemoryWitchContext(s any) (any, error) {
	//if _, err := mem.SwapMemoryWithContext(); err != nil {
	//	return nil, err
	//}
	return nil, nil
}
